Auth();
//r_type判断用户注册类型
var r_type = globalFun.GetQueryString('v_type');

$('.left_type p').on('click', function() {
	$(this).addClass('choose');
	$(this).siblings().removeClass('choose');
	var x = $(this).index();
	$('#m_right').children('div').eq(x).css('display', 'block');
	$('#m_right').children('div').eq(x).siblings().css('display', 'none')
})

//获取用户信息
//我的首页*********************************************************************************************
var mineObj = {
	loginName: name,
	uuid: uuid,
	loginType: is_team,
	loginId: id
}
$.ajax({
	type: "post",
	url: apicom + "/user/getMemberInfo",
	async: true,
	data: mineObj,
	success: function(res) {
		var info = res.data.memberInfo;
		$('.user_img img').attr('src', res.data.imgUrl);
		$('.name').text(info.realName);
		$('.user_num').text(info.number);
		$('.user_creattime').text(info.createTime);
		$('.user_logintime').text(res.data.loginTime);
		$('.user_phone').text(info.phone);
		$('.user_name').text(info.username);
		$('.user_mail').text(info.email);
		$('.user_address').text(info.address);
		$('td').eq(0).text(info.serviceHour + '个小时');
		$('td').eq(1).text(res.data.starLevel);
		$('td').eq(2).text(res.data.teamCount);
		$('td').eq(3).text(info.score);
		$('td').eq(4).html(info.serviceCount + '次');
		$('#v_username').val(info.username);
		$('#v_mail').val(info.email);
		$('#v_phone').val(info.phone);
		$('#v_address').val(info.address);
		$("#school_type").find("option[value='" + info.education + "']").attr("selected", true);
		$("#job_type").find("option[value='" + info.jobStatus + "']").attr("selected", true);
		$("#address_type").find("option[value='" + info.areaId + "']").attr("selected", true);
		//服务类别
		var server_type = $("#v_server_type input"); //获取所有的复选框值
		//放的复选框的值为字符串类型
		var express = info.serviceType.split(',');
		console.log(express)
		for(var i = 0; i < express.length; i++) {
			$.each(server_type, function(j, checkbox) {
				//获取复选框的value属性
				var checkValue = $(checkbox).val();
				if(express[i] == checkValue) {
					$(checkbox).attr("checked", true);
				}
			})
		}
		//服务领域回显
		var server_job = $("#server_job input");
		var express1 = info.serviceScope.split(',');
		for(var i = 0; i < express1.length; i++) {
			$.each(server_job, function(j, checkbox) {
				//获取复选框的value属性
				var checkValue = $(checkbox).val();
				if(express1[i] == checkValue) {
					$(checkbox).attr("checked", true);
				}
			})
		}
		$('#vl_img').attr('src', res.data.imgUrl)

	},
	error: function(err) {
		console.log(err)
	}
});
//修改资料*********************************************************************************************
var v_username;
var reg0 = /^[a-zA-Z0-9_-]{4,16}$/;
var bol0 = true;
v_username = $("#v_username").val();
console.log(v_username)
$("#v_username").on('change', function() {
	if($("#v_username").val() == '') {
		$(this).next().text('用户名不能为空').css('color', 'red').css('display', 'block');
		bol0 = false;
	} else if(reg0.test($("#v_username").val()) == false) {
		$(this).next().text("用户名格式不正确，请重新输入").css('color', 'red').css('display', 'block');
		bol0 = false;
	} else {
		v_username = $("#v_username").val();
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		bol0 = true;
	};
});
//手机号码
var v_phone;
var reg5 = /^1(3|4|5|7|8|9)\d{9}$/;
var bol1 = true;
v_phone = $("#v_phone").val();
$("#v_phone").on('change', function() {
	if($("#v_phone").val() == '') {
		$(this).next().text('手机号不能为空').css('color', 'red').css('display', 'block');
		bol1 = false;
	} else if(reg5.test($("#v_phone").val()) == false) {
		$(this).next().text("手机号格式不正确").css('color', 'red').css('display', 'block');
		bol1 = false;
	} else {
		v_phone = $("#v_phone").val();
		console.log(v_phone)
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		bol1 = true;
	};
});
//邮箱
var v_mail;
var reg2 = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
var bol2 = true;
v_mail = $("#v_mail").val();
$("#v_mail").on('change', function() {
	if($("#v_mail").val() == '') {
		$(this).next().text('邮箱不能为空').css('color', 'red').css('display', 'block');
		bol2 = false;
	} else if(reg2.test($("#v_mail").val()) == false) {
		$(this).next().text("电子邮箱格式不正确，请重新输入").css('color', 'red').css('display', 'block');
		bol2 = false;
	} else {
		v_mail = $("#v_mail").val();
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		bol2 = true;
	};
});
//详细地址
var v_address;
var bol3 = true;
v_address = $("#v_address").val();
console.log(v_address)
$("#v_address").on('change', function() {
	console.log($("#v_address").val())
	if($("#v_address").val() == '') {
		$(this).next().text('详细地址不能为空').css('color', 'red').css('display', 'block');
		bol3 = false;
	} else {
		v_address = $("#v_address").val();
		console.log(v_address)
		bol3 = true;
	};
});
$("#v_server_type input:checkbox").click(function() {
	var radio_len = $("#v_server_type input:checked").length;
	if(radio_len > 3) {
		$("#v_server_type").next().text('最多选择3个').css('color', 'red').css('display', 'block');
		return false;
	} else {
		$("#v_server_type").next().text('');
	}
})
$('#keep_info').on('click', function() {
	var volunteerObj = {};
	if(bol0 && bol1 && bol2 && bol3) {
		volunteerObj.uuid = uuid;
		volunteerObj.loginId = id;
		volunteerObj.loginType = is_team;
		volunteerObj.username = v_username;
		volunteerObj.phone = v_phone;
		volunteerObj.email = v_mail;
		volunteerObj.address = v_address;
		//		volunteerObj.longitude = longitude;
		//		volunteerObj.latitude = latitude;
		//学历
		var school_type = $("#school_type  option:selected").text();
		if(school_type == '请选择' || school_type == undefined) {
			tips('请选择最高学历');
			return false;
		} else {
			volunteerObj.education = school_type;
		};
		//工作状况
		var job_type = $("#job_type  option:selected").text();
		if(job_type == '请选择' || job_type == undefined) {
			tips('请选择就业状况');
			return false;
		} else {
			volunteerObj.jobStatus = job_type;
		};
		//服务区域
		var address_type = $("#address_type  option:selected").val();
		if(address_type == '请选择' || address_type == undefined) {
			tips('请选择服务区域');
			return false;
		} else {
			volunteerObj.areaId = address_type;
		};
		//服务类型
		var fk = [];
		var ffkk = '';
		var sl = $("#v_server_type input:checked").length;
		if(sl == 0) {
			tips('请选择服务领域或行业')
		} else {
			for(var i = 0; i < sl; i++) {
				fk.push($("#v_server_type input:checked").eq(i).val())
			}
			if(fk.length == 1) {
				ffkk = fk.join()
			} else if(fk.length > 1) {
				ffkk = fk.join(',')
			}
			volunteerObj.serviceType = ffkk
		}
		//服务领域或行业
		var hy = [];
		var hhyy = '';
		var gt = $("#server_job input:checked").length;
		if(gt == 0) {
			tips('请选择服务领域或行业')
		} else {
			for(var i = 0; i < gt; i++) {
				hy.push($("#server_job input:checked").eq(i).val())
			}
			if(hy.length == 1) {
				hhyy = hy.join()
			} else if(hy.length > 1) {
				hhyy = hy.join(',')
			}
			volunteerObj.serviceScope = hhyy
		}
		//用户图像
		if(vlImg == '' || vlImg == undefined) {
			volunteerObj.imgUrl = $('#vl_img').attr('src');
		} else {
			volunteerObj.imgUrl = vlImg;
		}
		console.log(volunteerObj)
		//调取注册借口		
		$.ajax({
			type: "post",
			url: apicom + "/user/editMemberInfo",
			async: false,
			dataType: "json",
			data: volunteerObj,
			success: function(res) {
				if(res.code == 200) {
					tips('注册成功，请前往登录')
				} else {
					tips(res.message)
				}
			},
			error: function(err) {

			}
		});
	} else {
		tips('请查看注册信息是否填写完整')
	}
})
//修改密码*********************************************************************************************
var v_passward;
var reg1 = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
var pbol_1 = false;
$("#v_passward").on('blur', function() {
	if($("#v_passward").val() == '') {
		$(this).next().text('密码不能为空').css('color', 'red').css('display', 'block');
		pbol_1 = false;
	} else if(reg1.test($("#v_passward").val()) == false) {
		$(this).next().text("密码格式不正确，请重新输入").css('color', 'red').css('display', 'block');
		pbol_1 = false;
	} else {
		v_passward = $("#v_passward").val();
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		pbol_1 = true;
	};
});
var v_passward1;
var reg1 = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
var pbol_2 = false;
$("#v_passward1").on('blur', function() {
	if($("#v_passward1").val() == '') {
		$(this).next().text('密码不能为空').css('color', 'red').css('display', 'block');
		pbol_2 = false;
	} else if(reg1.test($("#v_passward1").val()) == false) {
		$(this).next().text("密码格式不正确，请重新输入").css('color', 'red').css('display', 'block');
		pbol_2 = false;
	} else {
		v_passward1 = $("#v_passward1").val();
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		pbol_2 = true;
	};
});
var v_passward2;
var reg1 = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
var pbol_3 = false;
$("#v_passward2").on('blur', function() {
	if($("#v_passward2").val() == '') {
		$(this).next().text('密码不能为空').css('color', 'red').css('display', 'block');
		pbol_3 = false;
	} else if(reg1.test($("#v_passward2").val()) == false) {
		$(this).next().text("密码格式不正确，请重新输入").css('color', 'red').css('display', 'block');
		pbol_3 = false;
	} else {
		v_passward2 = $("#v_passward2").val();
		$(this).next().text('输入正确').css('color', 'limegreen').css('display', 'block');
		pbol_3 = true;
	};
});
$('#ch_password').on('click', function() {
	var relpassObj = {};
	if(pbol_1 && pbol_2 && pbol_3) {
		relpassObj.uuid = uuid;
		relpassObj.loginId = id;
		relpassObj.loginType = is_team;
		relpassObj.oldPassword = v_passward;
		relpassObj.newPassword = v_passward2;
		//调取注册借口		
		$.ajax({
			type: "post",
			url: apicom + "/user/editPassword",
			async: false,
			dataType: "json",
			data: relpassObj,
			success: function(res) {
				if(res.code == 200) {
					tips('密码修改成功')
				} else {
					tips(res.message)
				}
			},
			error: function(err) {

			}
		});
	} else {
		tips('请查看注册信息是否填写完整')
	}
})
//我的团队*********************************************************************************************
team()

function team() {
	var myTeam = {}
	myTeam.uuid = uuid;
	myTeam.loginId = id;
	myTeam.loginType = is_team;
	$.ajax({
		type: "post",
		url: apicom + "/user/getMyTeamInfo",
		async: false,
		dataType: "json",
		data: myTeam,
		success: function(res) {
			if(res.code == 200) {
				var content = res.data.teamList
				var ss = '';
				for(var i = 0; i < content.length; i++) {
					if(content[i].status == '正常') {
						ss += '<tr><td>' + content[i].teamName + '</td><td>' + content[i].telPhone + '</td><td>' + content[i].joinTime + '</td><td>' + content[i].status + '</td><td><span class="outTime" data-id="' + content[i].id + '">退出</span></td></tr>'
					} else {
						ss += '<tr><td>' + content[i].teamName + '</td><td>' + content[i].telPhone + '</td><td>' + content[i].joinTime + '</td><td>' + content[i].status + '</td><td><span data-id="' + content[i].id + '">已退出</span></td></tr>'
					}

				}
				$('.my_team').html(ss)
			} else {
				tips(res.message)
			}
		},
		error: function(err) {

		}
	});
}

//退出团队
$('.my_team').on('click', '.outTime', function() {
	var outId = $(this).attr('data-id')
	console.log(outId);
	var outObj = {}
	outObj.uuid = uuid;
	outObj.operateId = outId;
	outObj.loginType = is_team;
	$.ajax({
		type: "post",
		url: apicom + "/user/exit",
		async: false,
		dataType: "json",
		data: outObj,
		success: function(res) {
			if(res.code == 200) {
				team()
			} else {
				tips(res.message)
			}
		},
		error: function(err) {

		}
	});
})
//我的活动*************************************************************************************************
active()

function active() {
	var myActive = {}
	myActive.uuid = uuid;
	myActive.loginId = id;
	myActive.loginType = is_team;
	$.ajax({
		type: "post",
		url: apicom + "/user/myActivity",
		async: false,
		dataType: "json",
		data: myActive,
		success: function(res) {
			if(res.code == 200) {
				var content = res.data
				var ss = '';
				for(var i = 0; i < content.length; i++) {
					if(content[i].status == '已结束') {
						ss += '<tr><td>' + content[i].title + '</td><td>' + content[i].telphone + '</td><td>' + content[i].beginTime + '</td><td>' + content[i].serviceHour + '</td><td>' + content[i].status + '</td>'
						if(content[i].applyFlag == 1) {
							ss += '<td><span class="sqTime mark" h-name="' + content[i].title + '" t-id="' + content[i].teamId + '"  h-id="' + content[i].activityId + '">申请时长</span></td></tr>'
						} else {
							ss += '<td><span class="sqTime" h-name="' + content[i].title + '" t-id="' + content[i].teamId + '"  h-id="' + content[i].activityId + '">申请时长</span></td></tr>'
						}

					} else {
						ss += '<tr><td>' + content[i].title + '</td><td>' + content[i].telphone + '</td><td>' + content[i].beginTime + '</td><td>' + content[i].serviceHour + '</td><td>' + content[i].status + '</td><td></td></tr>'
					}

				}
				$('.my_active').html(ss)
			} else {
				tips(res.message)
			}
		},
		error: function(err) {

		}
	});
}
//申请时长
var activityId;
var activityName;
var teamId;
$('.my_active').on('click', '.sqTime', function() {
	if($(this).hasClass('mark')) {
		$('.mengban').show();
		$('.tips').html('您已提交过申请，正在审核中，请耐心等待');
	} else {
		activityId = $(this).attr('h-id');
		activityName = $(this).attr('h-name');
		teamId = $(this).attr('t-id');
		$('.mengban1').show();
		$('#active_name').html(activityName);
	}
})
//提交申请
$('.time_close').on('click', function() {
	var number = $('#active_num').val();
	var applyTime = $('#hours').val();
	var remark = $('#msg').val()
	if(number == '' || number == undefined) {
		tips('团队编号不能为空')
		return false;
	}
	if(applyTime == '' || applyTime == undefined) {
		tips('申请时长不能为空')
		return false;
	}
	var timeObj = {
		memberId: id,
		activityId: activityId,
		activityName: activityName,
		remark: remark,
		applyTime: applyTime,
		number: number,
		teamId: teamId
	}
	$.ajax({
		type: "post",
		url: apicom + "/user/applyDuration",
		async: false,
		dataType: "json",
		data: timeObj,
		success: function(res) {
			if(res.code == 200) {

			} else {
				tips(res.message)
			}
		},
		error: function(err) {

		}
	});
})

//站内信**********************************************************************************************
v_msg(1, true)

function v_msg(page, init) {
	var hdObj = {
		uuid: uuid,
		username: name,
		loginId: id,
		loginType: is_team,
		pageNum: page,
		pageSize: 7,
	}
	$.ajax({
		type: "post",
		url: apicom + '/sysmessage/mailbox',
		async: true,
		data: hdObj,
		success: function(res) {
			if(typeof res == 'string') {
				res = JSON.parse(res);
			}
			//			console.log(res)
			if(res.code == 200) {
				console.log(res)
				var dataScourse = res.data;
				if(res.pages == 1) {
					$(".v_page").hide();
					//					return false;
					jiegou(dataScourse);
				} else {
					jiegou(dataScourse);
					$(".v_page").show();
					/* 分页 */
					if(init == true) {
						$(".v_page").createPage({
							pageNum: res.pages, //总页码
							current: 1, //当前页
							shownum: 7, //每页显示个数
							showPageNum: false, //显示总页数
							showToPage: false, //显示跳转页码
							backfun: function(e) {
								v_msg(e.current, false);
							}
						});
					}
				}

			} else {
//				tips('数据暂无')
			}
		},
		error: function(err) {
			console.log(err)
		}
	})
}
//查看消息
var tId;
var aId;
$('.msg_info').on('click', '.watch', function() {
	tId = $(this).attr('tId');
	aId = $(this).attr('hId');
	if(aId == '' || aId == undefined || aId == "undefined") {
		$('.m_join').css('display', 'none')
	} else {
		$('.m_join').css('display', 'block')
	}
	$('.mengban2').show()
	$('.m_title').html($(this).attr('title'));
	$('.m_detail').html($(this).attr('content'));
})
//个人报名
$('.m_join').on('click', function() {
	var joinObj = {
		activityId: aId,
		uuid: uuid,
		username: name,
		loginId: id,
		loginType: is_team,
		teamId: tId
	}
	$.ajax({
		type: "post",
		url: apicom + "/afterActivity/enlistActivity",
		async: true,
		data: joinObj,
		success: function(res) {
			if(res.code == 200) {
				$('.mengban2').css('display', 'none');
				$('.mengban').css('display', 'block');
				$('.tips').html('报名成功，请等待录用')
			} else if(res.code == 0) {
				$('.mengban2').css('display', 'none');
				$('.mengban').css('display', 'block');
				$('.tips').html(res.message)
			}

		},
		error: function(err) {
			//				conosle.log(err)
		}
	});
})
//删除站内信
$('.msg_info').on('click', '.delete', function() {
	var msgId = $(this).attr('delid');
	$.ajax({
		type: "post",
		url: apicom + "/sysmessage/removeMessage",
		async: true,
		data: {
			uuid: uuid,
			username: name,
			loginId: id,
			loginType: is_team,
			messageId: msgId
		},
		success: function(res) {
			if(res.code == 200) {
				$('.mengban').css('display', 'block');
				$('.tips').html('删除成功')
				v_msg(1, true)
			}
		},
		error: function(err) {

		}
	});
})
//关系转换***************************************************************************************
$('#btnToteam').on('click', function() {
	var teamName = $('#toTname').val();
	var reason = $('#toTeamdetail').val();
	var remark = $('#msg').val()
	if(teamName == '' || teamName == undefined) {
		tips('团队名称不能为空')
		return false;
	}
	if(reason == '' || reason == undefined) {
		tips('转换原因不能为空')
		return false;
	}
	var toTeamObj = {
		uuid: uuid,
		loginId: id,
		loginType: is_team,
		teamName: teamName,
		reason: reason,
		type: 2
	}
	$.ajax({
		type: "post",
		url: apicom + "/apply/joinTeam",
		async: true,
		data: toTeamObj,
		success: function(res) {
			if(res.code == 200) {
				$('#toTname').val('');
				$('#toTeamdetail').val('');
				$('.mengban').css('display', 'block');
				$('.tips').text('申请已提交，请耐心等待')
			} else if(res.code == 0) {
				$('#toTname').val('');
				$('#toTeamdetail').val('');
				$('.mengban').css('display', 'block');
				$('.tips').text(res.message)
			}

		},
		error: function(err) {
			console.log(err)
		}
	});
})
//获取服务区域
$.ajax({
	type: "post",
	url: apicom + "/area/getAreaInfo",
	async: true,
	success: function(res) {
		var data = res.data;
		var ss = '';
		for(var i = 0; i < data.length; i++) {
			ss += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
		}
		$("#address_type").html(ss);
		$("#t_address_type").html(ss);
	},
	error: function(err) {

	}
});

function jiegou(content) {
	var ss = '';
	for(var i = 0; i < content.length; i++) {
		ss += '<tr><td>' + content[i].messageTitle + '</td><td>' + content[i].messageContent + '</td><td>' + content[i].senderName + '</td><td>' + content[i].createdTime + '</td><td><span tId="' + content[i].teamId + '" hId="' + content[i].activityId + '" content="' + content[i].messageContent + '" title="' + content[i].messageTitle + '" class="watch">查看</span>&nbsp;&nbsp;<span delId="' + content[i].messageId + '" class="delete">删除</span></td></tr>'
	}
	$('.msg_info').html(ss)
}
//关闭按钮
$('.btn_close').on('click', function() {
	$('.mengban').css('display', 'none');
})
$('.close_s').on('click', function() {
	$('.mengban').css('display', 'none');
})
$('.close_s1').on('click', function() {
	$('.mengban1').css('display', 'none');
})
$('.close_s2').on('click', function() {
	$('.mengban2').css('display', 'none');
})
$('.m_close').on('click', function() {
	$('.mengban2').hide()
})